'''
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License") you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
'''
from base.datatypes.xsd import XSDDatatypeConstants
from base.datatypes.xsd.impl.XSDAbstractDateTimeType import XSDAbstractDateTimeType
from base.shared.DatatypeFormatException import DatatypeFormatException


class XSDDayTimeDurationType(XSDAbstractDateTimeType):
    # param
    # return
    def __init__(self):
        super().__init__("duration")
        self.uri = XSDDatatypeConstants.XSD + "#dayTimeDuration"
    # XSDDayTimeDurationType():
    #     super("duration")
    #     super.uri = XSD + "#dayTimeDuration"
    

    #@Override
    # param String
    # return Object
    def parse(self, lex):
        # Object
        obj = super().parse(lex)
        # Must not have Y
        if (lex.indexOf('Y') != -1):
            # has year.
            raise DatatypeFormatException("Not valid as xsd:dayTimeDuration: " + lex)

        # 两个int
        idx_T = lex.indexOf('T')
        idx_M = lex.indexOf('M')

        if (idx_T == -1):
            # There is no T must have D and no M.
            if (lex.indexOf('D') == -1):
                raise DatatypeFormatException("Not valid as xsd:dayTimeDuration: " + lex)
            if (idx_M != -1):
                # M, no T -> month
                raise DatatypeFormatException("Not valid as xsd:dayTimeDuration: " + lex)
            return obj
        

        # Has T
        # Must not have a M before T
        if (idx_M != -1 and idx_M < idx_T):
            # M before T => month.
            raise DatatypeFormatException("Not valid as xsd:dayTimeDuration: " + lex)
        return obj
    

